//
//  EPMTopView.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/5/2.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMTopView.h"
#import "EPMCategoryView.h"
#import "EPMCategoryModel.h"

@interface EPMTopView()
@property (weak,nonatomic) UIView* coverView;
@property (strong,nonatomic) NSMutableArray* btnList;
@property (weak,nonatomic) UIButton* selectedBtn;
@property (weak,nonatomic)  UIButton* priceBtn;
@property (weak,nonatomic)  UIButton* upBtn;
@property (weak,nonatomic)  UIButton* downBtn;
@property (weak,nonatomic)  EPMCategoryView* categoryView;
@end
@implementation EPMTopView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        [self setupUI];
        self.btnList = [NSMutableArray array];
    }
    return self;
}

#pragma mark - 设置UI -
- (void)setupUI{
    CGFloat margin = 20;
    self.backgroundColor = [UIColor whiteColor];
    UIButton* synBtn = [[UIButton alloc]init];
    [self addSubview:synBtn];
    [synBtn setTitle:@"综合排序" forState:UIControlStateNormal];
    synBtn.tag = 1;
    [self.btnList addObject:synBtn];
    //默认选中
    synBtn.selected = YES;
    self.selectedBtn = synBtn;
    synBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [synBtn setTitleColor:appColor forState:UIControlStateSelected];
    [synBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [synBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(margin);
        make.top.equalTo(self).offset(10);
    }];
    [synBtn sizeToFit];
    
    UIButton* priceBtn = [[UIButton alloc]init];
    [self addSubview:priceBtn];
    self.priceBtn = priceBtn;
    priceBtn.tag = 2;
    [self.btnList addObject:priceBtn];
    [priceBtn setTitle:@"按价格" forState:UIControlStateNormal];
    priceBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [priceBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [priceBtn setTitleColor:appColor forState:UIControlStateSelected];
    [priceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(synBtn.mas_trailing).offset(margin);
        make.centerY.width.equalTo(synBtn);
        
    }];
    UIButton* upBtn = [[UIButton alloc]init];
    [upBtn setImage:[UIImage imageNamed:@"price_increse"] forState:UIControlStateNormal];
    [self addSubview:upBtn];
    upBtn.tag = 4;
    self.upBtn = upBtn;
    
    [upBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(priceBtn.mas_trailing);
        make.bottom.equalTo(priceBtn.mas_centerY);
        make.width.height.mas_equalTo(10);
    }];
    
    UIButton* downBtn = [[UIButton alloc]init];
    [downBtn setImage:[UIImage imageNamed:@"price_decrese"] forState:UIControlStateNormal];
    [self addSubview:downBtn];
    self.downBtn = downBtn;
    
    [downBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(upBtn);
        make.top.equalTo(upBtn.mas_bottom);
        make.width.height.mas_offset(5);
    }];
    
    UIButton* countBtn = [[UIButton alloc]init];
    [self addSubview:countBtn];
    countBtn.tag = 3;
    [self.btnList addObject:countBtn];
    [countBtn setTitle:@"按数量" forState:UIControlStateNormal];
    [countBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [countBtn setTitleColor:appColor forState:UIControlStateSelected];
    countBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [countBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(upBtn.mas_trailing).offset(margin);
        make.centerY.equalTo(priceBtn);
        make.width.equalTo(priceBtn);
        make.trailing.equalTo(self).offset(-margin);
    }];
    
    //点击事件添加
    
    [synBtn addTarget:self action:@selector(actionSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
    [priceBtn addTarget:self action:@selector(actionSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
    [countBtn addTarget:self action:@selector(actionSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //添加分割线
    UIView* lineView = [[UIView alloc]init];
    [self addSubview:lineView];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(10);
        make.trailing.offset(0);
        make.top.equalTo(synBtn.mas_bottom).offset(10);
        make.height.mas_equalTo(1);
    }];
    
    
    //分类按钮
    EPMCategoryView* categoryView = [[EPMCategoryView alloc]init];
    //测试颜色
//    categoryView.backgroundColor = [UIColor redColor];
    
    
    
    [self addSubview:categoryView];
    
    
    [categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.top.equalTo(lineView.mas_bottom).offset(10);
        make.bottom.equalTo(self);
    }];
    
    [categoryView setUpdateConstraintBlock:^(BOOL isEmpty) {
        [self layoutIfNeeded];
    }];
    
    
}


#pragma mark - 价格选择 -


- (void)actionPriceBtnClick:(UIButton*)sender{
    
    if (self.selectedBtn.tag != 2){
        self.selectedBtn.selected = NO;
        self.selectedBtn = self.priceBtn;
        _priceBtn.selected = YES;
    }
    
    if (sender == _upBtn){
        _upBtn.selected = YES;
        _downBtn.selected = NO;
    }else{
        _upBtn.selected = NO;
        _downBtn.selected = YES;
    }
    
    
}

#pragma mark - 按钮点击 -

- (void)actionSelectBtn:(UIButton*)sender{
    if(sender != self.selectedBtn){
        self.selectedBtn.selected = NO;
        sender.selected = YES;
        self.selectedBtn = sender;
    }
    
    
    
    
    
}



@end
